<template>
  <div>
    角色管理

    <div v-if="isShow" class="box">
      box
    </div>
    <button @click="toggleDiv">
      点击控制div显示
    </button>
    <el-button type="primary" size="default" @click="dialogVisible = !dialogVisible">新增</el-button>
    <el-dialog
      title="编辑角色"
      :visible.sync="dialogVisible"
      width="70%"
    >
      <el-form ref="form" :model="form" label-width="80px" :inline="false" size="normal">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" />
        </el-form-item>
        <el-form-item label="角色编码">
          <el-input v-model="form.roleCode" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.remark" />
        </el-form-item>
        <el-form-item label="树形菜单">
          <div>
            <el-checkbox v-model="isClose" @change="toogleClose">展开/关闭</el-checkbox>
            <el-checkbox v-model="isAllSelected" @change="toggleSelected">全选/反选</el-checkbox>

          </div>
          <el-tree
            ref="tree"
            :data="list"
            show-checkbox
            node-key="id"

            :default-checked-keys="defaultCheckedKeys"
            :props="defaultProps"
            @check="checkChange"
            @node-expand="nodeClick"
            @node-collapse="nodeClick"
          />
        </el-form-item>
        <el-form-item /></el-form-item></el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  findAllPermissions,
  saveRolePermissions
} from '@/api/auth/roles/index'
export default {
  data() {
    return {
      isShow: false,
      dialogVisible: false,
      list: [],
      defaultProps: {
        label: 'title'
      },
      isClose: false,
      isAllSelected: false,
      form: {
        'createTime': '',
        'id': '',
        'isDeleted': '',
        'modifyTime': '',
        'permissionIds': [
          ''
        ],
        'remark': '',
        'roleCode': '',
        'roleName': ''
      },
      defaultCheckedKeys: [], // 设置选中的树节点的id
      allIds: [] // 存放所有的id
    }
  },
  computed: {

  },
  created() {
    findAllPermissions()
      .then(res => {
        console.log(res)
        this.list = res.data.menus
        this.getAllId(res.data.menus)
      })
  },
  mounted() {

  },
  methods: {
    confirm() {
      console.log(this.$refs.tree.getCheckedKeys())
      this.form.permissionIds = this.$refs.tree.getCheckedKeys()
      saveRolePermissions(this.form)
        .then(res => {
          this.dialogVisible = !this.dialogVisible
        })
    },
    toggleDiv() {
      this.isShow = !this.isShow
      // dom操作 是异步的操作
      this.$nextTick(() => {
        console.log(document.querySelector('.box'))
        document.querySelector('.box').innerHTML = '张三'
      })
    },
    nodeClick() {
      var nodesMap = this.$refs.tree.store.nodesMap
      // 拿到的展开的节点组成的数组
      console.log(Object.values(nodesMap).filter(ele => ele.expanded))
      // console.log(Object.values(nodesMap).filter(ele => ele.expanded).length == this.allIds.length)
      this.$nextTick(() => {
        this.isClose = Object.values(nodesMap).filter(ele => ele.expanded).length == this.allIds.length
      })
    },
    toogleClose() {
      var nodesMap = this.$refs.tree.store.nodesMap
      Object.values(nodesMap).forEach(ele => {
        ele.expanded = this.isClose
      })
      console.log(this.$refs.tree.store.nodesMap)
    },
    checkChange(val, val1) { //
      console.log(val, val1)
      this.isAllSelected = val1.checkedKeys.length == this.allIds.length
    },
    toggleSelected() {
      this.$refs.tree.setCheckedKeys(this.isAllSelected ? this.allIds : [])
      // this.isAllSelected ? this.defaultCheckedKeys = this.allIds : this.defaultCheckedKeys = []
    },
    getAllId(val) { // 遍历数据  拿到数组每一项的id  存放到allIds
      if (!Array.isArray(val)) {
        return
      }
      val.forEach(ele => {
        this.allIds.push(ele.id)
        this.getAllId(ele.children)
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
